<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Log;
// use Dcat\Admin\Widgets\Form;
class PowerInstallationController extends Controller
{
    //5.1-智能变频脉冲电源安装质量检查记录表
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$this->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('5.1-智能变频脉冲电源安装质量检查记录表')
            ->description('分步表单');
    }
    protected function form()
{
    Admin::js('/stepform/js/jquery.smartWizard.min.js');
    Admin::css('/stepform/css/step.css');
    return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('电除尘改造信息调查表数据录入');
            $form->action('PowerInstallationstep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember() 
                ->padding('30px 18px 30px')
                ->add('主控项目', function (StepForm $step) use ($form) {
                    // $step->radio('pa1','柜的接地或接零，接地电阻小于2欧姆')->options(
                    //     ['1' => '接地可靠，阻值满足要求','2' => '接零可靠']
                    // );
                    $step->text('projectname',trans('项目名称'))->required();
                    $step->text('guihao',trans('柜号'))->required();
                    $step->text('sgdw',trans('施工单位'))->required();
                    $step->text('jcry',trans('检查人员'))->required();
                    $step->checkbox('pa1', '柜的接地或接零，接地电阻小于2欧姆')->options(['1'=>'接地可靠，阻值满足要求', '2'=>'接零可靠'])->default(1);
                    $step->checkbox('pa2', '熔断器及开关型号、规格是否符合设计要求')->options(['1'=>'符合设计要求'])->default(1);
                    $step->checkbox('pa3', '柜内小铜排及缆线螺栓是否紧固牢靠')->options(['1'=>'连接牢靠'])->default(1);
                    $step->checkbox('pa4', '终端头的连接，与开关、母线连接，必须接触紧密，用0.05*10mm塞尺检查，接触面宽50mm及以下，塞入深度不大于4mm，接触面宽50mm以上塞入深度不大于6mm，且连接牢靠。')
                    ->options(['1'=>'终端头制作满足要求','2'=>'终端接触满足要求'])->default(1);
                    $step->checkbox('pa5', '中间接头的连接，采用合格的连接管压接，压接时两芯线应碰上，套管内部不能空着，每侧压三次以上。采用热缩套管进行绝缘，热缩管平滑。')
                    ->options(['1'=>'中间接头满足要求','2'=>'热缩管满足要求'])->default(1);
                    $step->checkbox('pa6', '有中间接头电缆，根据电缆耐压等级选用1000v摇表，对电缆进行遥测，阻值应不小于10MΩ。')
                    ->options(['1'=>'阻值满足要求'])->default(1);
                    $step->checkbox('pa7', '缆线穿线套管应无毛刺。电缆与柜子本体绑扎固定时及进柜转弯处，可采用电缆皮垫加一层。')
                    ->options(['1'=>'缆线固定牢靠','2'=>'穿线管无毛刺'])->default(1);
                    $step->checkbox('pa8', '断路器进线端，相间灭弧绝缘橡胶板无缺失。')
                    ->options(['1'=>'符合要求'])->default(1);
                    $step->checkbox('pa9', 'IGBT螺栓锁紧，插线无松动')
                    ->options(['1'=>'符合要求'])->default(1);
                    $step->checkbox('pa10', '检查完毕，在开关出线处装上相序测量器，送电检查相序。')
                    ->options(['1'=>'相序正确'])->default(1);
                    $step->checkbox('pas11', '二次反馈屏蔽电缆，屏蔽层可靠接地。')
                    ->options(['1'=>'屏蔽层可靠接地'])->default(1);
                })
                ->add('一般项目', function ($step) {       
                    $step->checkbox('pas12', '柜子开箱，检查无变形，触摸板无损坏。')
                    ->options(['1'=>'满足要求'])->default(1);
                    $step->checkbox('pas13', '基础型钢，顶部平直度偏差1mm/米，且全长允许偏差5mm。')
                    ->options(['1'=>'偏差在范围内'])->default(1);
                    $step->checkbox('pas14', '相邻两盘柜垂直度允许偏差2mm，成排盘柜允许偏差5mm。相邻两盘柜平面度，允许偏差1mm，成排盘柜允许偏差5mm。')
                    ->options(['1'=>'垂直度满足偏差要求','2'=>'平面度满足偏差要求'])->default(1);
                    $step->checkbox('pas15', '盘柜与基础型钢连接应牢靠。')
                    ->options(['1'=>'连接牢靠'])->default(1);
                    $step->checkbox('pas16', '送电运行，各风扇转向正确，向外吹风。')
                    ->options(['1'=>'转向正确'])->default(1);
                })
                ->add('检查评定结果', function ($step) {
                    $step->text('zksum',trans('主控项目总数'))->required()->type('number');
                    $step->text('zktrue',trans('主控合格数'))->required()->type('number');
                    $step->text('zkfalse',trans('主控不合格数'))->required()->type('number');
                    $step->text('ybsum',trans('一般项目总数'))->required()->type('number');
                    $step->text('ybtrue',trans('一般项目合格数'))->required()->type('number');
                    $step->text('ybfalse',trans('一般项目不合格数'))->required()->type('number');
                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            Log::info($info);
            //实例化 phpword 类
            $PHPWord = new PhpWord();
            //指定事先制作好的模板文件路径
            $templateProcessor = new TemplateProcessor(public_path("filemodel/power_installation.docx"));
            $templateProcessor->setValue('projectname', $info['projectname']);
            $templateProcessor->setValue('guihao', $info['guihao']);
            $templateProcessor->setValue('sgdw', $info['sgdw']);
            $templateProcessor->setValue('jcry', $info['jcry']);
            $true = '<w:r><w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:sym w:font="Wingdings" w:char="00FE"/></w:r>';
            $false = '<w:r><w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:sym w:font="Wingdings" w:char="00A8"/></w:r>';
            $pa11 = $info['pa1'][0]==1?$true:$false;
            $pa12 = $info['pa1'][1]==1?$true:$false;
            $templateProcessor->setValue('pa11', $pa11);
            $templateProcessor->setValue('pa12', $pa12);
            $pa2 = $info['pa2'][0]==1?$true:$false;
            $templateProcessor->setValue('pa2', $pa2);
            $pa3 = $info['pa3'][0]==1?$true:$false;
            $templateProcessor->setValue('pa3', $pa3);
            $pa41 = $info['pa4'][0]==1?$true:$false;
            $pa42 = $info['pa4'][1]==1?$true:$false;
            $templateProcessor->setValue('pa41', $pa41);
            $templateProcessor->setValue('pa42', $pa42);
            $pa51 = $info['pa5'][0]==1?$true:$false;
            $pa52 = $info['pa5'][1]==1?$true:$false;
            $templateProcessor->setValue('pa51', $pa51);
            $templateProcessor->setValue('pa52', $pa52);
            $pa6 = $info['pa6'][0]==1?$true:$false;
            $templateProcessor->setValue('pa6', $pa6);
            $pa71 = $info['pa7'][0]==1?$true:$false;
            $pa72 = $info['pa7'][1]==1?$true:$false;
            $templateProcessor->setValue('pa71', $pa71);
            $templateProcessor->setValue('pa72', $pa72);
            $pa8 = $info['pa8'][0]==1?$true:$false;
            $templateProcessor->setValue('pa8', $pa8);
            $pa9 = $info['pa9'][0]==1?$true:$false;
            $templateProcessor->setValue('pa9', $pa9);
            $pa10 = $info['pa10'][0]==1?$true:$false;
            $templateProcessor->setValue('pa10', $pa10);
            $pas11 = $info['pas11'][0]==1?$true:$false;
            $templateProcessor->setValue('pas11', $pas11);
            $pas12 = $info['pas12'][0]==1?$true:$false;
            $templateProcessor->setValue('pas12', $pas12);
            $pas13 = $info['pas13'][0]==1?$true:$false;
            $templateProcessor->setValue('pas13', $pas13);
            $pas141 = $info['pas14'][0]==1?$true:$false;
            $pas142 = $info['pas14'][1]==1?$true:$false;
            $templateProcessor->setValue('pas141', $pas141);
            $templateProcessor->setValue('pas142', $pas142);
            $pas15 = $info['pas15'][0]==1?$true:$false;
            $templateProcessor->setValue('pas15', $pas15);
            $pas16 = $info['pas16'][0]==1?$true:$false;
            $templateProcessor->setValue('pas16', $pas16);

            $templateProcessor->setValue('zksum',$info['zksum']);
            $templateProcessor->setValue('zktrue',$info['zktrue']);
            $templateProcessor->setValue('zkfalse',$info['zkfalse']);
            $templateProcessor->setValue('ybsum',$info['ybsum']);
            $templateProcessor->setValue('ybtrue',$info['ybtrue']);
            $templateProcessor->setValue('ybfalse',$info['ybfalse']);

            
            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = $info['projectname'].date('Y-m-d H:i');
            $data['data'] = $info;
            // $data['content'] = json_encode($info);
            // $insertData['content'] = json_encode($data);
            $insertData['name'] = $data['name'];
            $insertData['type'] = 2;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            $templateProcessor->saveAs(public_path("filemodellist/".$data['name'].".docx"));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }

    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }

    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');;
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }
}